//战役回顾事件循环，模拟战役循环，重现过去

var reviewLoop = {
	playing : false,
	
	//当前战役状态保存
	_campaign_sav : null,
	_forceInfo_sav : null,
	
	//第一个回合不需要更新时间和势力
	_isFirstLoop : false,
	
	_timeoutHandler : null,
	
	Initialize : function(){
		this.playing = false;
		this._campaign_save = null;
		this._forceInfo_sav = null;
		this._isFirstLoop = false;
		this._timeoutHandler = null;
	},
	
	Review : function(start_step){
		//保存旧的战役状态，生成新的战役状态
		reviewLoop._campaign_sav = DeepClone(campaign);
		campaign = DeepClone(campaign_ori);

		reviewLoop._forceInfo_sav = DeepClone(forceInfo);
		var forceCapture = new ForceCapture(campaign, tileEngine);
		BuildForceInfo(campaign, tileEngine, forceCapture);
		
		UnLoadManipulateEnvironment();
		control.battleReviewMenu.enabled = true;

		//更新战役状态到 start_step 回合
		while (campaign.time_ref.current_step < start_step){
			SimulateJudge();
			
			TurnNewStep();
		}
		
		reviewLoop._isFirstLoop = true;
		
		//开始循环播放
		reviewLoop.playing = true;
		reviewLoop.EndStep();
	},
	
	EndStep : function(){
		if (reviewLoop.playing == false)
			return;
		reviewLoop.playing = false;
		
		processAnimation.Stop();
		cc.log("review " + campaign.time_ref.current_step + ".4: animation end, stepEnd start");
		EndOldStep();

		if (campaign.time_ref.current_step < (reviewLoop._campaign_sav.time_ref.current_step - 1)
				|| reviewLoop._isFirstLoop){
			reviewLoop._timeoutHandler = setTimeout(reviewLoop.StartStep, campaignScene.canvaslayer.showDuration * 1000);
		}else{
			reviewLoop._timeoutHandler = setTimeout(reviewLoop.FinishReview, campaignScene.canvaslayer.showDuration * 1000);
		}
	},
	
	StartStep : function(){
		if (reviewLoop.playing)
			return;
		reviewLoop.playing = true;
		
		if (reviewLoop._isFirstLoop){
			reviewLoop._isFirstLoop = false;
		}else{
			cc.log("review " + campaign.time_ref.current_step +  ".5: stepEnd end, turn new step");
			TurnNewStep();
		}
		cc.log("review " + campaign.time_ref.current_step + ".1: stepStart start");
		StartNewStep();
		
		reviewLoop._timeoutHandler = setTimeout(reviewLoop.GetProcess, campaignScene.canvaslayer.hideDuration * 1000);
	},

	GetProcess : function(){
		if (reviewLoop.playing == false)
			return;
		
		cc.log("review " + campaign.time_ref.current_step + ".2: stepStart end, simulate-judge start");
		SimulateJudge();
		
		reviewLoop.PlayProcess();
	},
	
	PlayProcess : function(){
		cc.log("review " + campaign.time_ref.current_step + ".3: simulate-judge end, animation start");
		processAnimation.Play();
		reviewLoop._timeoutHandler = setTimeout(reviewLoop.EndStep, (processAnimation.duration + 3.3) * 1000);
	},
	
	StopReviewLoop : function(){
		if (reviewLoop._timeoutHandler != null){
			clearTimeout(reviewLoop._timeoutHandler);
			reviewLoop._timeoutHandler = null;
		}
		if (reviewLoop.playing){
			//负责强制结束的处理,EndStep会再次调用本函数
			processAnimation.Stop();

			campaign.time_ref.current_step = reviewLoop._campaign_sav.time_ref.current_step - 1;
			reviewLoop._isFirstLoop = false;
			reviewLoop.EndStep();
			return;
		}
	},
	
	FinishReview : function(){
		
		reviewLoop.StopReviewLoop();
		
		//恢复原本的战役状态
		campaign = DeepClone(reviewLoop._campaign_sav);
		forceInfo = DeepClone(reviewLoop._forceInfo_sav);

		reviewMenu.showing = false;
		
		//重新布置战场
		StartNewStep();
		LoadManipulateEnvironment();
	}
};